package ua.at.javac.cron;

//import java.math.BigDecimal;
//import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import ua.at.javac.server.Database;

/**
 * Обработка фактической погоды
 * 
 * @author Michael.Michaelovich
 * @version 68
 * @since 2012
 */

public class SortURLCurrentWeather {
	
	public void start() throws Exception{
		
		Calendar cal = new GregorianCalendar();
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");   
		
		Database db = new Database();
		city = db.getSiteCity("gismeteo");
		for(String[] c : city){
			String httpLink = c[3];
			period_id = 4;
			sbuf0 = new Reader(httpLink).readUrl();
		
		allData = sbuf0.substring(sbuf0.indexOf(startPoint), sbuf0.indexOf(endPoint));
		int startPointCloudiness = allData.indexOf(cloudinessStr)+cloudinessStr.length();
		int endPointCloudiness = allData.indexOf(quote, startPointCloudiness);
		//System.out.println(allData);
		cloudinessStrOut = allData.substring(startPointCloudiness, endPointCloudiness);
		
		//System.out.println("Облачность: "+cloudinessStrOut);		//Облачность
		
		int startPointTemperature = allData.indexOf(temperatureStr) + temperatureStr.length();
		int endPointTemperature = allData.indexOf(less, startPointTemperature);
		temperatureStrOut = allData.substring(startPointTemperature, endPointTemperature);
		temperatureStrOut = temperatureStrOut.replace("&minus;", "-");
	if(temperatureStrOut.charAt(0)=='+')
		temperatureStrOut = allData.substring(startPointTemperature+1, endPointTemperature);
	
		//System.out.println("Температура: "+temperatureStrOut);		// Температура
	
		int startPointWindSpeed = allData.indexOf(windSpeedStr, endPointTemperature)+windSpeedStr.length();
		int endPointWindSpeed = allData.indexOf(less, startPointWindSpeed);
		windSpeedOut = allData.substring(startPointWindSpeed, endPointWindSpeed);
	
		//System.out.println("Скорость ветра: "+windSpeedOut);
		
		int startPointWind = allData.indexOf(teg, endPointWindSpeed)+teg.length();
		int enPointWind = allData.indexOf(less, startPointWind);
		wind_idStr = allData.substring(startPointWind, enPointWind);
		
		//System.out.println("Направление ветра: "+wind_idStr);		//Направление ветра
	
		int startPointPressure = allData.indexOf(pressureStr) + pressureStr.length();
		int endPointPressure = allData.indexOf(less, startPointPressure);
		pressureStrOut = allData.substring(startPointPressure, endPointPressure);
		
		//System.out.println("Давление: "+pressureStrOut);			// Давление
		
		int startPointHumidity = allData.indexOf(humidityStr) + humidityStr.length();
		int endPointHumidity = allData.length();
		humidityStrOut = allData.substring(startPointHumidity, endPointHumidity);
		
		//System.out.println("Влажность: "+humidityStrOut);			// Влажность
		
		// преобразование для направления ветра
		if (wind_idStr.equals("С"))wind_id = 1;
		if (wind_idStr.equals("СВ"))wind_id = 2;
		if (wind_idStr.equals("В"))wind_id = 3;
		if (wind_idStr.equals("ЮВ"))wind_id = 4;
		if (wind_idStr.equals("Ю"))wind_id = 5;
		if (wind_idStr.equals("ЮЗ"))wind_id = 6;
		if (wind_idStr.equals("З"))wind_id = 7;
		if (wind_idStr.equals("СЗ"))wind_id = 8;
		if (wind_idStr.equals("Ш"))wind_id = 9;		//Добавить в базу "ШТИЛЬ"!!!!!!!!!!! - OK!
		
		if (cloudinessStrOut.equals("Ясно"))cloudiness=0;
		if (cloudinessStrOut.equals("Пасмурно"))cloudiness=25;
		if (cloudinessStrOut.equals("Облачно"))cloudiness=45;
		if (cloudinessStrOut.equals("Пасмурно, дымка"))cloudiness=50;
		if (cloudinessStrOut.equals("Пасмурно, небольшой дождь"))cloudiness=75;
		if (cloudinessStrOut.equals("Пасмурно, небольшой снег"))cloudiness=75;
		if (cloudinessStrOut.equals("Облачно, снег"))cloudiness=90;								//Пасмурно; Облачно; Пасмурно, небольшой дождь; 
																								//Малооблачно, дождь; Пасмурно, дождь; Облачно, небольшой дождь 
		temperature = Integer.parseInt(temperatureStrOut);										//Ясно; Малооблачно; Малооблачно, небольшой дождь;
		humidity = Integer.parseInt(humidityStrOut);											//Малооблачно, сильный снег; Малооблачно, небольшой снег; 
		pressure = Integer.parseInt(pressureStrOut);											//Облачно, дождь; 
		windSpeed = Integer.parseInt(windSpeedOut);
		upToDate = f.format(cal.getTime());
		hours = cal.get(Calendar.HOUR_OF_DAY);
		period_id = timeOfDay(hours);
		//System.out.println(period_id+" "+hours);
		db.addToForecast(upToDate, period_id, cloudiness, temperature, 0.0, 
				humidity, pressure, windSpeed, wind_id);			
	   }
 }
	
	private int timeOfDay(int hour) {
		int id_of_day = 0;
		
		switch (hour){
		case 0: id_of_day = 4 ;
		 	break;
		case 1: id_of_day = 4 ;
	 		break;
		case 2: id_of_day = 4 ;
	 		break;
		case 3: id_of_day = 4 ;
	 		break;
		case 4: id_of_day = 4 ;
	 		break;
		case 5: id_of_day = 4 ;
	 		break;
		case 6: id_of_day = 1 ;
	 		break;
		case 7: id_of_day = 1 ;
	 		break;
		case 8: id_of_day = 1 ;
	 		break;
		case 9: id_of_day = 1 ;
	 		break;
		case 10: id_of_day = 1 ;
	 		break;
		case 11: id_of_day = 1 ;
	 		break;
		case 12: id_of_day = 2 ;
	 		break;
		case 13: id_of_day = 2 ;
	 		break;
		case 14: id_of_day = 2 ;
	 		break;
		case 15: id_of_day = 2 ;
	 		break;
		case 16: id_of_day = 2 ;
	 		break;
		case 17: id_of_day = 2 ;
	 		break;
		case 18: id_of_day = 3 ;
	 		break;
		case 19: id_of_day = 3 ;
	 		break;
		case 20: id_of_day = 3 ;
	 		break;
		case 21: id_of_day = 3 ;
	 		break;
		case 22: id_of_day = 3 ;
	 		break;
		case 23: id_of_day = 3 ;
		
		default: System.out.println("ERROR!");
	}
		return id_of_day;
}
 private List<String[]> city;   
 private String startPoint = "<h2>Погода за окном</h2>";
 private String endPoint = "<span class=\"unit\">%";
 private String cloudinessStr = "<dt class=\"png\" title=\"";
 private String temperatureStr = "<dd class='value m_temp c'>";
 private String teg = "<dt>";
 private String quote = "\""; 
 private String pressureStr = "<dd class='value m_press torr'>";
 private String windSpeedStr = "<dd class='value m_wind ms'>";
 private String less = "<";
 private String allData;
 private StringBuffer sbuf0;
 private String temperatureStrOut;
 private String humidityStr = "title=\"Влажность\">";
 private String upToDate;
 private String pressureStrOut;
 private String windSpeedOut;
 private String wind_idStr;
 private String cloudinessStrOut;
 private String humidityStrOut;
 //private double precipitation;
 private int hours;
 private int period_id;
 private int cloudiness;
 private int wind_id = 1;
 private int temperature;
 private int humidity;
 private int pressure;
 private int windSpeed;
}
